﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BizzCommInterfaces;
using System.IO;
using System.Windows.Forms;

namespace FileTrays
{
    public class FileOutbox : Tray, IOutbox
    {
        //private const string _name = "FileOutboxProvider";
        private const char _separator = ';';
        
        public string folder { get; set; }
        //private string concreteOutboxName;
         public FileOutbox()
        {
            folder = null;
            base._name = "FileOutboxProvider";
             
        }

        #region IOutbox Members

        public void getConfigurationWindow()
        {
            new FileOutboxConfigControl(this).ShowDialog();
            
        }

        public int getCount()
        {
            var files = Directory.GetFiles(folder).Where(name => name.EndsWith(".txt"));
            return files.Count();
        }

        public void writeMessage(BizzMessage msg)
        {
            writeMsg(msg);
        }
        
        #endregion

        private void writeMsg(BizzMessage msg)
        {
            string path = @folder;
            string name = string.Format(msg.Title + "-{0:yyyy-MM-dd_hh-mm-ss-tt}.txt", DateTime.Now);
            path += "\\" + name;
            if (!File.Exists(path))
            {
                // Create a file to write to. 
                TextWriter tw = File.CreateText(path);

                tw.WriteLine(msg.Title);
                foreach (var att in msg.Attributes)
                {
                    tw.Write(att.Name); tw.Write(";");
                    tw.Write(msg.setMsgType(att.Type)); tw.Write(";");
                    tw.Write(att.Value);
                    tw.WriteLine();
                }
                tw.Close();
            }
        }


    

      

        public string[] getDescription()
        {
            string[] ret = new string[8];
            ret[0] = "Direcotrio Texto. Cada mensaje será un archivo txt";
            ret[1] = "en el directorio que se seleccione.";
            ret[2] = "El formato del txt será:";
            ret[3] = "";
            ret[4] = "este es el titulo del mensaje";
            ret[5] = "Apellido;Texto;Perez";
            ret[6] = "Edad;Numero;30";
            ret[7] = "Casado;Booleano;True";


            return ret;
        }

      
        

        #region IOutbox Members

        public string codeMe()
        {
            string ret = folder + _separator + _concreteName;
            return ret;
        }

        public void decodeMe(string box)
        {
            string[] attr = box.Split(_separator);
            folder = attr[0];
            _concreteName = attr[1];
        }

        public bool isReady()
        {
            return (folder != null);
        }

        #endregion

        #region IOutbox Members


        public string getConcreteOutboxName()
        {
            return _concreteName;
        }

        public string getOutboxName()
        {
            return _name;
        }

        public void setConcreteOutboxName(string name)
        {
            _concreteName = name;
        }

        #endregion
    }
}
